Contact messages scaffold

jamesperet 9 years ago
parent
commit
8d0ff07cf4

+ 1 - 0
Gemfile

@@ -36,6 +36,7 @@ gem 'flatstrap-rails'
36 36
 gem 'font-awesome-rails'
37 37
 gem 'bootstrap-timepicker-rails'
38 38
 gem 'simple_form'
39
+gem 'bootstrap_form'
39 40
 gem 'friendly_id', '~> 5.0.0'
40 41
 gem 'devise'
41 42
 gem "letter_opener", :group => :development

+ 2 - 0
Gemfile.lock

@@ -30,6 +30,7 @@ GEM
30 30
     bcrypt (3.1.7)
31 31
     bootstrap-timepicker-rails (0.1.3)
32 32
       railties (>= 3.0)
33
+    bootstrap_form (2.2.0)
33 34
     builder (3.1.4)
34 35
     carrierwave (0.10.0)
35 36
       activemodel (>= 3.2.0)
@@ -190,6 +191,7 @@ PLATFORMS
190 191
 
191 192
 DEPENDENCIES
192 193
   bootstrap-timepicker-rails
194
+  bootstrap_form
193 195
   carrierwave
194 196
   coffee-rails (~> 4.0.0)
195 197
   devise

+ 3 - 0
app/assets/javascripts/contact_messages.js.coffee

@@ -0,0 +1,3 @@
1
+# Place all the behaviors and hooks related to the matching controller here.
2
+# All this logic will automatically be available in application.js.
3
+# You can use CoffeeScript in this file: http://coffeescript.org/

+ 1 - 0
app/assets/stylesheets/application.css

@@ -11,6 +11,7 @@
11 11
  *= require_self
12 12
  *= require flatstrap/bootstrap
13 13
  *= require font-awesome
14
+ *= require rails_bootstrap_forms
14 15
  *= require summernote
15 16
  *= require_tree .
16 17
  */

+ 4 - 0
app/controllers/admin_panel_controller.rb

@@ -16,6 +16,10 @@ class AdminPanelController < ApplicationController
16 16
      @posts = BlogPost.all
17 17
   end
18 18
 
19
+  def contact_messages
20
+    @contact_messages = ContactMessage.all
21
+  end
22
+
19 23
   def users
20 24
     @users = User.all
21 25
   end

+ 63 - 0
app/controllers/contact_messages_controller.rb

@@ -0,0 +1,63 @@
1
+class ContactMessagesController < ApplicationController
2
+  before_action :set_contact_message, only: [:show, :edit, :update, :destroy]
3
+
4
+  # GET /contact_messages
5
+  # GET /contact_messages.json
6
+  def index
7
+    @contact_message = ContactMessage.new
8
+  end
9
+
10
+  # POST /contact_messages
11
+  # POST /contact_messages.json
12
+  def create
13
+    @contact_message = ContactMessage.new(contact_message_params)
14
+    if user_signed_in?
15
+      @contact_message.user = current_user
16
+    end
17
+    @contact_message.unread = true
18
+    respond_to do |format|
19
+      if @contact_message.save
20
+        format.html { redirect_to @contact_message, notice: 'Contact message was successfully created.' }
21
+        format.json { render action: 'show', status: :created, location: @contact_message }
22
+      else
23
+        format.html { render action: 'new' }
24
+        format.json { render json: @contact_message.errors, status: :unprocessable_entity }
25
+      end
26
+    end
27
+  end
28
+
29
+  # PATCH/PUT /contact_messages/1
30
+  # PATCH/PUT /contact_messages/1.json
31
+  def update
32
+    respond_to do |format|
33
+      if @contact_message.update(contact_message_params)
34
+        format.html { redirect_to @contact_message, notice: 'Contact message was successfully updated.' }
35
+        format.json { head :no_content }
36
+      else
37
+        format.html { render action: 'edit' }
38
+        format.json { render json: @contact_message.errors, status: :unprocessable_entity }
39
+      end
40
+    end
41
+  end
42
+
43
+  # DELETE /contact_messages/1
44
+  # DELETE /contact_messages/1.json
45
+  def destroy
46
+    @contact_message.destroy
47
+    respond_to do |format|
48
+      format.html { redirect_to contact_messages_url }
49
+      format.json { head :no_content }
50
+    end
51
+  end
52
+
53
+  private
54
+    # Use callbacks to share common setup or constraints between actions.
55
+    def set_contact_message
56
+      @contact_message = ContactMessage.find(params[:id])
57
+    end
58
+
59
+    # Never trust parameters from the scary internet, only allow the white list through.
60
+    def contact_message_params
61
+      params.require(:contact_message).permit(:title, :email, :content, :unread, :user_id)
62
+    end
63
+end

+ 2 - 0
app/helpers/contact_messages_helper.rb

@@ -0,0 +1,2 @@
1
+module ContactMessagesHelper
2
+end

+ 3 - 0
app/models/contact_message.rb

@@ -0,0 +1,3 @@
1
+class ContactMessage < ActiveRecord::Base
2
+  belongs_to :user
3
+end

+ 9 - 0
app/views/admin_panel/_sidebar_nav.html.erb

@@ -1,14 +1,23 @@
1 1
 <div class="span3" style="margin-top: 25px;">	
2 2
 	<ul class="nav nav-pills nav-stacked">
3
+	   
3 4
 	   <% if current_page?(:action => 'dashboard')%><li class="active"> <% else %><li><% end %>
4 5
 	   <%= link_to ('<i class="icon-home icon-white"></i> '+(t "admin_panel.dashboard")).html_safe, admin_dashboard_path %></li>
6
+	   
5 7
 	   <% if current_page?(:action => 'posts')%><li class="active"> <% else %><li><% end %>
6 8
 	   <%= link_to ('<i class="icon-file icon-white"></i> '+(t "admin_panel.posts")).html_safe, admin_posts_path %></li>
9
+	   
7 10
 	   <% if current_page?(:action => 'files')%><li class="active"> <% else %><li><% end %>
8 11
 	   <%= link_to ('<i class="icon-file icon-white"></i> '+(t "admin_panel.files")).html_safe, admin_files_path %></li>
12
+	   
13
+	   <% if current_page?(:action => 'contact_messages')%><li class="active"> <% else %><li><% end %>
14
+	   <%= link_to ('<i class="icon-envelope icon-white"></i> '+(t "admin_panel.messages")).html_safe, admin_contact_messages_path %></li>
15
+	   
9 16
 	   <% if current_page?(:action => 'users')%><li class="active"> <% else %><li><% end %>
10 17
 	   <%= link_to ('<i class="icon-user icon-white"></i> '+(t "admin_panel.users")).html_safe, admin_users_path %></li>
18
+	   
11 19
 	   <% if current_page?(:action => 'site_config')%><li class="active"> <% else %><li><% end %>
12 20
 	   <%= link_to ('<i class="icon-cog icon-white"></i> '+(t "admin_panel.configurations")).html_safe, admin_config_path %></li>
21
+	   
13 22
 	</ul>
14 23
 </div>

+ 27 - 0
app/views/admin_panel/contact_messages.html.erb

@@ -0,0 +1,27 @@
1
+<div class="row">
2
+	<%= render 'admin_panel/sidebar_nav' %>
3
+	<div class="span9">
4
+		<div class="page-header">
5
+		  <h1><%= t "admin_panel.contact_messages" %></h1>
6
+		</div>
7
+		<%= bootstrap_flash %>
8
+		<% @contact_messages.each do |msg| %>
9
+		<div class="media thumbnail" style="padding: 10px; padding-bottom: 5px;">
10
+		  <div class="media-body pull-left">
11
+		    <h3 class="media-heading" style="margin-bottom: 0px;"><%= link_to msg.title, contact_message_path(msg) %> 
12
+			    <%= ('<span class="badge badge-warning" style="margin-top: -11px">' + (t "admin_panel.new") + '</span>').html_safe if msg.unread %></h3>
13
+		    <p style="margin-top: -5px;"><small><%= t "blog.by" %> 
14
+			     <% if msg.user != nil %>
15
+			    		<%= msg.user.full_name %>
16
+				<% else %>
17
+					<%= msg.email %>
18
+				<% end %>
19
+				, <%= time_ago_in_words(msg.created_at) %> <%= t "blog.ago" %>
20
+		    </small></p>
21
+		    <p><%= msg.content %></p>
22
+		  </div> 
23
+		</div>
24
+		<% end %>
25
+		
26
+	</div>
27
+</div>

+ 14 - 0
app/views/contact_messages/_form.html.erb

@@ -0,0 +1,14 @@
1
+<%= bootstrap_form_for(@contact_message, layout: :default) do |f| %>
2
+	<%= f.alert_message (t 'contact.error') %>
3
+
4
+	<%= f.text_field :title, :label => (t 'contact.title'), :class => 'input-block-level' %>
5
+	<% if !user_signed_in? %>
6
+		<%= f.email_field :email, :class => 'input-block-level' %>
7
+	<% end %>
8
+	<%= f.text_area :content, :label => (t 'contact.message'), :class => 'input-block-level', rows: '6'  %>
9
+
10
+	<%= f.button :submit, :value => (t 'contact.send') %>
11
+	
12
+ 
13
+
14
+<% end %>

+ 13 - 0
app/views/contact_messages/index.html.erb

@@ -0,0 +1,13 @@
1
+<div class="page-header">
2
+	<h1><%= t 'contact.contact'%></h1>
3
+</div>
4
+
5
+
6
+<div class="row">
7
+<div class="well span4">
8
+<p><%= t 'contact.greeting1' %> <%= mail_to(@config.contact_email) %> <%= t 'contact.greeting2' %></p>
9
+
10
+<%= render 'form' %>
11
+</div>
12
+</div>
13
+

+ 4 - 0
app/views/contact_messages/index.json.jbuilder

@@ -0,0 +1,4 @@
1
+json.array!(@contact_messages) do |contact_message|
2
+  json.extract! contact_message, :id, :title, :email, :content, :unread, :user_id
3
+  json.url contact_message_url(contact_message, format: :json)
4
+end

+ 30 - 0
app/views/contact_messages/show.html.erb

@@ -0,0 +1,30 @@
1
+<p id="notice"><%= notice %></p>
2
+
3
+<p>
4
+  <strong>Title:</strong>
5
+  <%= @contact_message.title %>
6
+</p>
7
+
8
+<p>
9
+  <strong>Email:</strong>
10
+  <%= @contact_message.email %>
11
+</p>
12
+
13
+<p>
14
+  <strong>Content:</strong>
15
+  <%= @contact_message.content %>
16
+</p>
17
+
18
+<p>
19
+  <strong>Unread:</strong>
20
+  <%= @contact_message.unread %>
21
+</p>
22
+
23
+<% if @contact_message.user != nil %>
24
+	<p>
25
+	  <strong>User:</strong>
26
+	  <%= @contact_message.user.full_name  %>
27
+	</p>
28
+<% end %>
29
+
30
+<%= link_to 'Back', admin_contact_messages_path %>

+ 1 - 0
app/views/contact_messages/show.json.jbuilder

@@ -0,0 +1 @@
1
+json.extract! @contact_message, :id, :title, :email, :content, :unread, :user_id, :created_at, :updated_at

+ 1 - 0
app/views/layouts/_navigation_links.html.erb

@@ -1,6 +1,7 @@
1 1
 <div class="container nav-collapse">
2 2
   <ul class="nav">
3 3
     <li><%= link_to "Blog", blog_path  %></li>
4
+    <li><%= link_to (t 'contact.contact'), contact_messages_path  %></li>
4 5
   </ul>
5 6
   
6 7
 <ul class="nav pull-right">

+ 13 - 1
config/locales/en.yml

@@ -220,6 +220,9 @@ en:
220 220
     update_config_btn: Update Config
221 221
     contact_email: Contact Email
222 222
     config_update_success: Configurations saved successfully
223
+    contact_messages: Contact Messages
224
+    new: New
225
+    messages: Messages
223 226
   nav:
224 227
     admin_panel: Admin Panel
225 228
     account: Account
@@ -230,4 +233,13 @@ en:
230 233
     edit: Editar
231 234
   blog:
232 235
     by: By
233
-    ago: ago
236
+    ago: ago
237
+  contact:
238
+    contact: Contact
239
+    contact_messages: Contact Messages
240
+    greeting1: Send us an email at
241
+    greeting2: or write us a message
242
+    title: Message Title
243
+    message: Message
244
+    send: Send
245
+    error: Please correct the following errors

+ 12 - 1
config/locales/pt-BR.yml

@@ -222,6 +222,9 @@ pt-BR:
222 222
     default_language: Lingua padrão
223 223
     update_config_btn: Salvar Configurações
224 224
     contact_email: Email de contato
225
+    contact_messages: Mensagens de Contato
226
+    new: Nova
227
+    messages: Mensagens
225 228
   nav:
226 229
     admin_panel: Painel de Controle
227 230
     account: Conta
@@ -233,4 +236,12 @@ pt-BR:
233 236
   blog:
234 237
     by: Por
235 238
     ago: atras
236
-    
239
+  contact:
240
+    contact: Contato
241
+    contact_messages: Mensagens de Contato
242
+    greeting1: Entre em contato pelo email
243
+    greeting2: ou escreva uma mensagem
244
+    title: Assunto
245
+    message: Mensagem
246
+    send: Enviar
247
+    error: Favor corrigir os erros abaixo

+ 3 - 0
config/routes.rb

@@ -1,10 +1,13 @@
1 1
 RailsWebsiteTemplate::Application.routes.draw do
2 2
   
3
+  resources :contact_messages, path: '/contact'
4
+
3 5
   resources :uploads
4 6
 
5 7
   get "admin/dashboard" => "admin_panel#dashboard", :as => :admin_dashboard
6 8
   get "admin" => "admin_panel#index"
7 9
   get "admin/posts" => "admin_panel#posts", :as => :admin_posts
10
+  get "admin/contact_messages" => "admin_panel#contact_messages", :as => :admin_contact_messages
8 11
   get "admin/users" => "admin_panel#users", :as => :admin_users
9 12
   get "admin/config" => "admin_panel#site_config", :as => :admin_config
10 13
   post "admin/config/update" => "admin_panel#site_config_update", :as => :config_update

+ 13 - 0
db/migrate/20141024174901_create_contact_messages.rb

@@ -0,0 +1,13 @@
1
+class CreateContactMessages < ActiveRecord::Migration
2
+  def change
3
+    create_table :contact_messages do |t|
4
+      t.string :title
5
+      t.string :email
6
+      t.text :content
7
+      t.boolean :unread
8
+      t.references :user, index: true
9
+
10
+      t.timestamps
11
+    end
12
+  end
13
+end

+ 13 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20140924001609) do
14
+ActiveRecord::Schema.define(version: 20141024174901) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -31,6 +31,18 @@ ActiveRecord::Schema.define(version: 20140924001609) do
31 31
   add_index "blog_posts", ["author_id"], name: "index_blog_posts_on_author_id", using: :btree
32 32
   add_index "blog_posts", ["slug"], name: "index_blog_posts_on_slug", unique: true, using: :btree
33 33
 
34
+  create_table "contact_messages", force: true do |t|
35
+    t.string   "title"
36
+    t.string   "email"
37
+    t.text     "content"
38
+    t.boolean  "unread"
39
+    t.integer  "user_id"
40
+    t.datetime "created_at"
41
+    t.datetime "updated_at"
42
+  end
43
+
44
+  add_index "contact_messages", ["user_id"], name: "index_contact_messages_on_user_id", using: :btree
45
+
34 46
   create_table "friendly_id_slugs", force: true do |t|
35 47
     t.string   "slug",                      null: false
36 48
     t.integer  "sluggable_id",              null: false

+ 49 - 0
test/controllers/contact_messages_controller_test.rb

@@ -0,0 +1,49 @@
1
+require 'test_helper'
2
+
3
+class ContactMessagesControllerTest < ActionController::TestCase
4
+  setup do
5
+    @contact_message = contact_messages(:one)
6
+  end
7
+
8
+  test "should get index" do
9
+    get :index
10
+    assert_response :success
11
+    assert_not_nil assigns(:contact_messages)
12
+  end
13
+
14
+  test "should get new" do
15
+    get :new
16
+    assert_response :success
17
+  end
18
+
19
+  test "should create contact_message" do
20
+    assert_difference('ContactMessage.count') do
21
+      post :create, contact_message: { content: @contact_message.content, email: @contact_message.email, title: @contact_message.title, unread: @contact_message.unread, user_id: @contact_message.user_id }
22
+    end
23
+
24
+    assert_redirected_to contact_message_path(assigns(:contact_message))
25
+  end
26
+
27
+  test "should show contact_message" do
28
+    get :show, id: @contact_message
29
+    assert_response :success
30
+  end
31
+
32
+  test "should get edit" do
33
+    get :edit, id: @contact_message
34
+    assert_response :success
35
+  end
36
+
37
+  test "should update contact_message" do
38
+    patch :update, id: @contact_message, contact_message: { content: @contact_message.content, email: @contact_message.email, title: @contact_message.title, unread: @contact_message.unread, user_id: @contact_message.user_id }
39
+    assert_redirected_to contact_message_path(assigns(:contact_message))
40
+  end
41
+
42
+  test "should destroy contact_message" do
43
+    assert_difference('ContactMessage.count', -1) do
44
+      delete :destroy, id: @contact_message
45
+    end
46
+
47
+    assert_redirected_to contact_messages_path
48
+  end
49
+end

+ 15 - 0
test/fixtures/contact_messages.yml

@@ -0,0 +1,15 @@
1
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+one:
4
+  title: MyString
5
+  email: MyString
6
+  content: MyText
7
+  unread: false
8
+  user_id: 
9
+
10
+two:
11
+  title: MyString
12
+  email: MyString
13
+  content: MyText
14
+  unread: false
15
+  user_id: 

+ 4 - 0
test/helpers/contact_messages_helper_test.rb

@@ -0,0 +1,4 @@
1
+require 'test_helper'
2
+
3
+class ContactMessagesHelperTest < ActionView::TestCase
4
+end

+ 7 - 0
test/models/contact_message_test.rb

@@ -0,0 +1,7 @@
1
+require 'test_helper'
2
+
3
+class ContactMessageTest < ActiveSupport::TestCase
4
+  # test "the truth" do
5
+  #   assert true
6
+  # end
7
+end